package folderLogger.outputFile;

import java.io.PrintStream;

import folderLogger.OperationSettings;
import folderLogger.outputFile.formatting.OutputFileFormatter;
import folderLogger.outputFile.formatting.OutputFormatterFactory;

public abstract class OutputWriter {

	protected PrintStream outputFile;
	protected OutputFileFormatter fileFormatter;
	protected OutputFileFormat type;
	protected String resourcesFolder;
	
	public OutputWriter(OutputFileFormat type, String resourcesFolder)
	{
		this.type = type;
		this.resourcesFolder = resourcesFolder;
	}
	
	public int InitializeOutputFormatter()
	{
		fileFormatter = OutputFormatterFactory.getFormatter(type);
		if(fileFormatter == null)
		{
			//	Type is not supported!
			return -4;
		}
		fileFormatter.setResourcesFolder(resourcesFolder);
		return 0;
	}
	
	/**
	 * Initialises the output file.
	 * 
	 * @return 0 iff file was initialised successfully. negative value otherwise.
	 */
	public abstract int InitializeIndexFile();

	/**
	 * Flush and close the output stream
	 */
	public abstract void close();
		
	public boolean WriteFolderHeader(String folderName, FileAttributes attributes)
	{
		outputFile.print(fileFormatter.getPreFolder(folderName, attributes));
		return true;
	}
	
	public boolean WriteFolderFooter(String folderName, FileAttributes attributes) {
		outputFile.print(fileFormatter.getPostFolder(folderName, attributes));
		return true;
		
	}
	
	public boolean WriteFileHeader(String fileName, FileAttributes attributes)
	{
		outputFile.print(fileFormatter.getPreFile(fileName, attributes));
		return true;
	}
	
	public boolean WriteFileFooter(String fileName, FileAttributes attributes)
	{
		outputFile.print(fileFormatter.getPostFile(fileName, attributes));
		return true;
	}

	public boolean WriteHeader(String baseFolder, OperationSettings settings) {
		//	Write file header
		outputFile.print(fileFormatter.getHeader(baseFolder, settings));
		return true;
		
	}

	public boolean WriteFooter() {
		outputFile.print(fileFormatter.getFooter());
		return true;
	}

	/**
	 * Create all files/data necessary before main index file.
	 * 
	 * @param outputFolder: folder to store files
	 * @return status
	 */
	public boolean PreProcess(String outputFolder)
	{		
		String[] preProcessData = fileFormatter.getPreProcessData();
		if(preProcessData.length % 2 != 0)
		{
			return false;
		}
		for(int dataIndex = 0; dataIndex <  preProcessData.length/2; dataIndex++)
		{
			String currentName = preProcessData[2*dataIndex];
			String currentData = preProcessData[2*dataIndex+1];
			
			ExportDataStream(currentName, currentData);
		}
		return true;
	}

	/**
	 * Formatter may wish to export additional files except for the main index.
	 * Formatter supplies the data to store and the location to store it.
	 * This function receives a single data stream and stores it in the appropriate location.
	 * 
	 * @param streamName:	Name of current data stream
	 * @param streamData: 	Content of current data stream
	 * @return status
	 */
	protected abstract boolean ExportDataStream(String streamName, String streamData);
	
	
}
